﻿using AutoMapper;
using IntelligentAgriculture.Contracts.Dto.Input;
using IntelligentAgriculture.Contracts.Dto.Output;
using IntelligentAgriculture.Contracts.Interface;
using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.Domain.Interface;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentAgriculture.Services
{
    public class RoleService : IRoleService
    {
        private readonly IRoleRepository _roleRepository;
        private readonly ILogger<RoleService> _logger;
        private readonly IMapper _mapper;
        public RoleService(IRoleRepository roleRepository, ILogger<RoleService> logger, IMapper mapper)
        {
            _roleRepository = roleRepository;
            _logger = logger;
            _mapper = mapper;
        }
        /// <summary>
        /// 获取所有角色
        /// </summary>
        /// <returns></returns>
        public async Task<PageOutput<Role>> GetRoles(RoleInput input)
        {
            try
            {
                //获取到所有角色
                var list = await _roleRepository.GetRolesAsync();
                //根据角色名筛选
                if (!string.IsNullOrEmpty(input.RoleName))
                {
                    list = list.Where(p => p.Rname.Contains(input.RoleName)).ToList();
                }
                var result = new PageOutput<Role>()
                {
                    PageSize = input.PageSize,
                    TotalCount = list.Count,
                };
                result.Data = list.OrderBy(p => p.Rid).Skip((input.PageIndex - 1) * input.PageSize).Take(input.PageSize).ToList();
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError("获取角色分页信息失败" + ex.Message);
                throw ex;
            }
        }
        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="input"> 角色信息 </param>
        /// <returns></returns>
        public async Task<int> AddRole(AddRoleInput input)
        {
            try
            {
                //添加角色
                int n = await _roleRepository.AddRole(_mapper.Map<Role>(input));
                return n;
            }
            catch (Exception ex)
            {
                _logger.LogError("添加角色信息失败" + ex.Message);
                throw ex;
            }
        }
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="id"> 角色ID </param>
        /// <returns></returns>
        public async Task<int> DeleteRole(int id)
        {
            try
            {
                //删除角色
                int n = await _roleRepository.DeleteRole(id);
                return n;
            }
            catch (Exception ex)
            {
                _logger.LogError("删除角色信息失败" + ex.Message);
                throw ex;
            }
        }
        /// <summary>
        /// 获取角色选择列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<RoleSelectOutput>> GetRolesSelect()
        {
            try
            {
                var roles = await _roleRepository.GetRoleSelect();
                return _mapper.Map<List<RoleSelectOutput>>(roles);
            }
            catch (Exception ex)
            {
                _logger.LogError("获取角色选择列表失败" + ex.Message);
                throw ex;
            }
        }
    }
}